package com.example.crm.controller;

import com.example.crm.service.CustomerService;
import com.example.crm.service.OpportunityService;
import com.example.crm.service.ContractService;
import com.example.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据分析API控制器
 */
@RestController
@RequestMapping("/api/analytics")
public class AnalyticsAPIController {

    @Autowired
    private CustomerService customerService;
    
    @Autowired
    private OpportunityService opportunityService;
    
    @Autowired
    private ContractService contractService;

    /**
     * 获取所有统计数据
     */
    @GetMapping("/data")
    public Result<Map<String, Object>> getData() {
        Map<String, Object> data = new HashMap<>();
        
        // 获取客户统计数据
        data.put("customer", customerService.getCustomerStatistics());
        
        // 获取销售机会统计数据
        data.put("opportunity", opportunityService.getSalesStatistics());
        
        // 获取合同统计数据
        data.put("contract", contractService.getContractStatistics());
        
        return Result.success(data);
    }
    
    /**
     * 获取客户分析数据
     */
    @GetMapping("/customer")
    public Result<Map<String, Object>> getCustomerAnalytics() {
        return Result.success(customerService.getCustomerStatistics());
    }
    
    /**
     * 获取销售机会分析数据
     */
    @GetMapping("/opportunity")
    public Result<Map<String, Object>> getOpportunityAnalytics() {
        Map<String, Object> data = new HashMap<>();
        data.put("statistics", opportunityService.getSalesStatistics());
        data.put("funnel", opportunityService.getSalesFunnelData());
        return Result.success(data);
    }
    
    /**
     * 获取合同分析数据
     */
    @GetMapping("/contract")
    public Result<Map<String, Object>> getContractAnalytics() {
        Map<String, Object> data = new HashMap<>();
        data.put("statistics", contractService.getContractStatistics());
        data.put("statusDistribution", contractService.getContractStatusDistribution());
        data.put("amountTrend", contractService.getContractAmountTrend(6));
        return Result.success(data);
    }
} 